/*
*  cbtable.c - Callback hashtable functions
*  Copyright (c) 2005 Derek Ingrouville (dergro@users.sourceforge.net)
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Library General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "cbtable.h"

MsnCallbacks *
msn_callbacks_new (void)
{
    MsnCallbacks *msncbs = NULL;

    msncbs = g_new (MsnCallbacks, 1);
    msncbs->cmd_callbacks = g_hash_table_new (g_str_hash, g_str_equal);
    msncbs->msg_callbacks = g_hash_table_new (g_str_hash, g_str_equal);
    msncbs->error_callbacks = g_hash_table_new (g_str_hash, g_str_equal);
	msncbs->event_callbacks = g_hash_table_new (g_str_hash, g_str_equal);
    msncbs->cmd_data = NULL;
	msncbs->event_data = NULL;

    return msncbs;
}

void
msn_callbacks_free (MsnCallbacks *msncbs)
{
    g_return_if_fail (msncbs != NULL);

    if (msncbs->cmd_callbacks != NULL)
        g_hash_table_destroy (msncbs->cmd_callbacks);
    
    if (msncbs->msg_callbacks != NULL)
        g_hash_table_destroy (msncbs->msg_callbacks);
    
    if (msncbs->error_callbacks != NULL)
        g_hash_table_destroy (msncbs->error_callbacks);

	if (msncbs->event_callbacks != NULL)
        g_hash_table_destroy (msncbs->event_callbacks);

    g_free (msncbs);
}

void
msn_callbacks_add_cmd (MsnCallbacks *msncbs, char *cmd, gpointer cb)
{
    GHashTable *cbs = NULL;

    g_return_if_fail (msncbs != NULL);
    g_return_if_fail (cmd != NULL);
    g_return_if_fail (cb != NULL);
    
    cbs = msncbs->cmd_callbacks;
    g_hash_table_insert (cbs, cmd, cb);
}

void
msn_callbacks_add_msg (MsnCallbacks *msncbs, char *content_type, gpointer cb)
{
    GHashTable *cbs = NULL;

    g_return_if_fail (msncbs != NULL);
    g_return_if_fail (content_type != NULL);
    g_return_if_fail (cb != NULL);
    
    cbs = msncbs->msg_callbacks;
    g_hash_table_insert (cbs, content_type, cb);
}

void
msn_callbacks_add_error (MsnCallbacks *msncbs, char *error, gpointer cb)
{
    GHashTable *cbs = NULL;

    g_return_if_fail (msncbs != NULL);
    g_return_if_fail (error != NULL);
    g_return_if_fail (cb != NULL);
    
    cbs = msncbs->error_callbacks;
    g_hash_table_insert (cbs, error, cb);
}

void
msn_callbacks_add_event (MsnCallbacks *msncbs, char *event_name, gpointer cb)
{
    GHashTable *cbs = NULL;

    g_return_if_fail (msncbs != NULL);
    g_return_if_fail (event_name != NULL);
    g_return_if_fail (cb != NULL);
    
    cbs = msncbs->event_callbacks;
    g_hash_table_insert (cbs, event_name, cb);
}


void
msn_callbacks_set_cmd_data (MsnCallbacks *msncbs, gpointer data)
{
    g_return_if_fail (msncbs != NULL);
    msncbs->cmd_data = data;
}

void
msn_callbacks_set_event_data (MsnCallbacks *msncbs, gpointer data)
{
    g_return_if_fail (msncbs != NULL);
    msncbs->event_data = data;
}

MsnCallback
msn_callbacks_lookup (MsnCallbacks *msncbs, char *key)
{
	MsnCallback cb = NULL;

	g_return_val_if_fail (msncbs != NULL, NULL);
	g_return_val_if_fail (key != NULL, NULL);

	cb = g_hash_table_lookup (msncbs->cmd_callbacks, key);

	if (cb == NULL)
		cb = g_hash_table_lookup (msncbs->msg_callbacks, key);
	if (cb == NULL)
		cb = g_hash_table_lookup (msncbs->error_callbacks, key);
	if (cb == NULL)
		cb = g_hash_table_lookup (msncbs->event_callbacks, key);
	
	return cb;
}
